package com.yugao.fintech.libra.auth.api;

import com.alibaba.cloud.nacos.NacosServiceAutoConfiguration;
import com.yugao.fintech.libra.auth.api.model.entity.LocalUser;
import com.yugao.fintech.libra.auth.api.model.entity.OAuth2Client;
import com.yugao.fintech.libra.auth.api.model.entity.SocialUser;
import com.yugao.fintech.libra.auth.api.model.rqrs.CancelAccountReq;
import com.yugao.fintech.libra.auth.api.model.rqrs.CaptchaCheckReq;
import com.yugao.fintech.libra.auth.api.model.rqrs.LocalUserGetReq;
import com.yugao.fintech.libra.auth.api.model.rqrs.LocalUserListReq;
import com.yugao.fintech.libra.auth.api.model.rqrs.ldp.ExtLdpConnectResp;
import com.yugao.fintech.libra.auth.api.model.rqrs.third.LoginQrcodeInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@ConditionalOnClass({FeignAutoConfiguration.class, NacosServiceAutoConfiguration.class})
public class AuthApiCloudImpl implements AuthApi {
    @Autowired
    private AuthRemote service;

    @Override
    public List<ExtLdpConnectResp> listLdpConnectByClientId(String clientId) {
        Resp<List<ExtLdpConnectResp>> resp = service.listLdpConnectByClientId(clientId);
        RetOps.of(resp).assertSuccess(e -> new BusinessException(e.getCode(), e.getMsg()));
        return resp.getData();
    }

    @Override
    public void deleteLoginLog(Integer daysBefore) {
        Resp<?> rs = service.deleteLoginLog(daysBefore);
        RetOps.of(rs).assertSuccess(e -> new BusinessException(e.getCode(), e.getMsg()));
    }

    @Override
    public LoginUser getLoginUser() {
        Resp<LoginUser> resp = service.getLoginUser();
        RetOps.of(resp).assertSuccess(e -> new BusinessException(e.getCode(), e.getMsg()));
        return resp.getData();
    }

    @Override
    public void updateLoginUser(LoginUser loginUser) {
        Resp<Boolean> resp = service.updateLoginUser(loginUser);
        RetOps.of(resp).assertSuccess(e -> new BusinessException(e.getCode(), e.getMsg()));
    }

    @Override
    public boolean removeAccessTokenByUserId(Long userId) {
        Resp<Boolean> resp = service.removeAccessTokenByUserId(userId);
        RetOps.of(resp).assertSuccess(e -> new BusinessException(e.getCode(), e.getMsg()));
        return resp.getData();
    }

    @Override
    public boolean removeAccessTokenByUserIds(List<Long> userIds) {
        Resp<Boolean> resp = service.removeAccessTokenByUserIds(userIds);
        RetOps.of(resp).assertSuccess(e -> new BusinessException(e.getCode(), e.getMsg()));
        return resp.getData();
    }

    @Override
    public void deleteCaptcha(String uuid) {
        Resp<?> resp = service.deleteCaptcha(uuid);
        RetOps.of(resp).assertSuccess(e -> new BusinessException(e.getCode(), e.getMsg()));
    }

    @Override
    public boolean checkCaptcha(CaptchaCheckReq req) {
        Resp<Boolean> resp = service.checkCaptcha(req);
        RetOps.of(resp).assertSuccess(e -> new BusinessException(e.getCode(), e.getMsg()));
        return resp.getData();
    }

    @Override
    public LocalUser getUser(LocalUserGetReq req) {
        Resp<LocalUser> resp = service.getUser(req);
        RetOps.of(resp).assertSuccess((e) -> new BusinessException(e.getCode(), e.getMsg()));
        return resp.getData();
    }

    @Override
    public LocalUser getUserByUserId(Long userId) {
        Resp<LocalUser> resp = service.getUserByUserId(userId);
        RetOps.of(resp).assertSuccess((e) -> new BusinessException(e.getCode(), e.getMsg()));
        return resp.getData();
    }

    @Override
    public void updateLocalUser(LocalUser req) {
        Resp<?> resp = service.updateLocalUser(req);
        RetOps.of(resp).assertSuccess((e) -> new BusinessException(e.getCode(), e.getMsg()));
    }

    @Override
    public void deleteLocalUser(Long userId) {
        Resp<?> resp = service.deleteLocalUser(userId);
        RetOps.of(resp).assertSuccess((e) -> new BusinessException(e.getCode(), e.getMsg()));
    }

    @Override
    public void batchDeleteLocalUser(Long tenantId, List<Long> userIds) {
        Resp<?> resp = service.batchDeleteLocalUser(tenantId, userIds);
        RetOps.of(resp).assertSuccess((e) -> new BusinessException(e.getCode(), e.getMsg()));
    }

    @Override
    public Long saveLocalUser(LocalUser req) {
        Resp<Long> resp = service.saveLocalUser(req);
        RetOps.of(resp).assertSuccess((e) -> new BusinessException(e.getCode(), e.getMsg()));
        return resp.getData();
    }

    @Override
    public void batchUpdateLocalUser(List<LocalUser> req) {
        Resp<?> resp = service.batchUpdateLocalUser(req);
        RetOps.of(resp).assertSuccess((e) -> new BusinessException(e.getCode(), e.getMsg()));
    }

    @Override
    public void cancelAccount(CancelAccountReq req) {
        Resp<?> resp = service.cancelAccount(req);
        RetOps.of(resp).assertSuccess((e) -> new BusinessException(e.getCode(), e.getMsg()));
    }

    @Override
    public List<LocalUser> listAuth(LocalUserListReq query) {
        Resp<List<LocalUser>> resp = service.listAuth(query);
        RetOps.of(resp).assertSuccess((e) -> new BusinessException(e.getCode(), e.getMsg()));
        return resp.getData();
    }

    @Override
    public OAuth2Client getByClientId(String clientId) {
        Resp<OAuth2Client> resp = service.getByClientId(clientId);
        RetOps.of(resp).assertSuccess(e -> new BusinessException(e.getCode(), e.getMsg()));
        return resp.getData();
    }

    @Override
    public void saveThirdAuth(SocialUser saveData) {
        throw new UnsupportedOperationException("当前方法不支持远程调用");
    }

    @Override
    public void refreshClientAuthorityByRoleIds(List<Long> roleIds, boolean isDelete) {
        Resp<?> resp = service.refreshClientAuthorityByRoleIds(roleIds, isDelete);
        RetOps.of(resp).assertSuccess(e -> new BusinessException(e.getCode(), e.getMsg()));
    }

    @Override
    public void userScanQrcode(LoginQrcodeInfo req) {
        Resp<?> resp = service.userScanQrcode(req);
        RetOps.of(resp).assertSuccess(e -> new BusinessException(e.getCode(), e.getMsg()));
    }
}
